package de.homelabs.moonrat.config;

import javax.naming.Binding;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

/**
 * Logs out the configured JNDI-Properties. Mainly used for debugging purposes.
 * Uses the INFO level.
 * 
 * @author D.Mueller
 * 
 */
public class MoonratJndiPrinter {
	static Logger logger = Logger.getLogger(MoonratJndiPrinter.class);

	/**
	 * Log JNDI properties into the INFO Level
	 */
	public static void printJndiProperties(){
		printJndiProperties(Level.INFO);
	}
	
	/**
	 * Log JNDI properties into the given level
	 */
	public static void printJndiProperties(Level level) {
		try {
			logger.info("Printout JNDI-Properties");
			Context ctx = (Context) new InitialContext().lookup("java:comp/env");

			NamingEnumeration<Binding> list = ctx.listBindings("");
			while (list.hasMore()) {
				Binding item = list.next();
				String className = item.getClassName();
				String name = item.getName();
				Object value = item.getObject();
				logger.info(className + " " + name + ", value: "+value);

			}
		} catch (NamingException e) {
			logger.error("cannot printout JNDI-Parameters: " + e.getMessage());
		}

	}
}
